package com.quanyan.authority.web;

import com.lefu.commons.utils.Page;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.authority.model.db.TbMenu;
import com.quanyan.authority.model.request.ReqMenu;
import com.quanyan.authority.model.response.RespMenu;
import com.quanyan.authority.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/11.
 */
@RequestMapping("/admin/api/authority/menu/api")
@RestController
public class MenuController extends BaseController<ReqMenu> {

    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;

    @Autowired
    private HttpServletRequest request;

    @Override
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public APIResponse save(@RequestBody ReqMenu reqMenu, BindingResult bindingResult) {
        logger.info("增加菜单：{}",reqMenu.toString());
        try {
            Integer uid = Integer.parseInt(request.getAttribute("adminUid").toString());
            TbMenu tbMenu = menuService.add(reqMenu);
            Map<String,Object> map = new HashMap();
            map.put("tbMenu",tbMenu);
            map.put("userMenu",menuService.getUserMenuNew(uid));
            return APIResponse.returnSuccess(map);
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "query",method = RequestMethod.POST)
    public APIResponse query(@RequestBody ReqMenu reqMenu) {
        try {
            return APIResponse.returnSuccess( menuService.getMenuOne());
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @RequestMapping(value = "oneMenu",method = RequestMethod.POST)
    public APIResponse oneMenu(@RequestBody ReqMenu reqMenu) {
        try {
            return APIResponse.returnSuccess( menuService.getMenuOne());
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }


    @Override
    @RequestMapping(value = "queryById",method = RequestMethod.POST)
    public APIResponse queryById(@RequestBody Map map) {
        try {
            Integer id = Integer.valueOf(map.get("id").toString());
            if(id == null)
                return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
            return APIResponse.returnSuccess( menuService.findById(id));
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public APIResponse update(@RequestBody ReqMenu reqMenu) {
        try {
            Integer uid = Integer.parseInt(request.getAttribute("adminUid").toString());
            menuService.update(reqMenu);
            return APIResponse.returnSuccess(menuService.getUserMenuNew(uid));
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public APIResponse deleteById(@RequestBody Map map) {
        try {
            Integer uid = Integer.parseInt(request.getAttribute("adminUid").toString());
            Integer id = Integer.valueOf(map.get("id").toString());
            if(id == null)
                return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
            menuService.del(id);
            return APIResponse.returnSuccess(menuService.getUserMenuNew(uid));
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }


    @RequestMapping(value = "userMenu",method = RequestMethod.POST)
    public APIResponse userMenu(HttpServletRequest request){
        try {
            Integer uid =Integer.parseInt(request.getAttribute("adminUid").toString()) ;
            return APIResponse.returnSuccess(menuService.getUserMenuNew(uid));
        } catch (NumberFormatException e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常");
        }

    }

}
